package kale.adapter;

import android.support.annotation.Nullable;

import java.util.List;

public abstract class LemonRcvAdapter<T> extends CommonRcvAdapter<T> {
    protected LemonRcvAdapter(@Nullable List<T> data) {
        super(data);
    }

    //Add By me

    /**
     * Clear the list of the mAdapter
     *
     * @param list
     */
    public void clear(List<?> list) {
        int size = list.size();
        list.clear();
        notifyItemRangeRemoved(0, size);
    }


    public void addItem(T e, int index) {
        if (null != e) {
            this.mDataList.add(index, e);
            notifyItemInserted(index);
        }
    }

    public void addItem(T e) {
        if (null != e) {
            this.mDataList.add(e);
            int insertPosition = getItemCount() - 1;
            notifyItemInserted(insertPosition);
        }
    }

    public void addItems(int startPosition,List<T> items) {
        if (startPosition >= this.mDataList.size() || startPosition < 0) {
            startPosition = 0;
        }
        if (null != items && !items.isEmpty()) {
            int size = items.size();
            this.mDataList.addAll(startPosition, items);
            notifyItemRangeInserted(startPosition, size);
        }
    }

    public void addItems(List<T> items) {
        if (null != items && !items.isEmpty()) {
            int size      = items.size();
            int itemCount = getItemCount();
            this.mDataList.addAll(items);
            notifyItemRangeInserted(itemCount, size);
        }
    }

    public T removeLast() {
        if (mDataList.size() == 0) {
            return null;
        }
        int index = mDataList.size() - 1;
        T item = mDataList.remove(index);
        notifyItemRemoved(index);
        return item;
    }

    public T remove(int index) {
        T item = mDataList.remove(index);
        notifyItemRemoved(index);
        return item;
    }


    public void remove(T e) {
        if (null != e) {
            remove(mDataList.indexOf(e));
        }
    }

}
